import numpy as np
import os

from plot.geom import plotAirfoilGeom

if __name__ == "__main__":

    '''
        -r /home/linjx/pycharmworkspace/visual/visual_airfoil/airfoil_geom_mlcls -s /home/linjx/pycharmworkspace/visual/visual_airfoil/plot
        -r /home/linjx/pycharmworkspace/visual/visual_airfoil/airfoil_geom_clsVSnotcls -s /home/linjx/pycharmworkspace/visual/visual_airfoil/plot
    '''

    # 命令行参数
    # parser = argparse.ArgumentParser()
    # parser.add_argument("-r", "--read_dir", type=str, help="待处理翼型所在文件夹路径", required=True)
    # parser.add_argument("-s", "--save_dir", type=str, help="处理后的翼型保存路径", required=True)
    # args = parser.parse_args()

    # 反设计
    InvOrPst = r"InverseDesign"
    pic_name_list = ["airfoil_geom_mlcls", "airfoil_geom_clsVSnotcls"]

    # 正设计
    # pic_name_list = ["airfoil_geom", "airfoil_geom"]
    # InvOrPst = r"PositiveDesign"

    read_dir_list = ["/home/linjx/pycharmworkspace/visual/visual_airfoil/{}/{}".format(InvOrPst, pic_name)
                     for pic_name in pic_name_list]
    save_dir = "/home/linjx/pycharmworkspace/visual/visual_airfoil/plot"
    save_pig_list = ["{}.png".format(pic_name) for pic_name in pic_name_list]

    # if os.path.exists(save_dir):
    #     shutil.rmtree(save_dir)
    # os.mkdir(save_dir)

    for read_dir, save_pig in zip(read_dir_list, save_pig_list):

        # 画多个翼型
        save_path = os.path.join(save_dir, save_pig)
        coordinates_list = []
        file_list = []

        # 文件名排序
        filenames = os.listdir(read_dir)
        filenames.sort(key=lambda x: int(x[0:2]))

        # 遍历并读出所有翼型文件
        for file in filenames:
            print(file)
            file_path = os.path.join(read_dir, file)
            coordinates = np.loadtxt(file_path)
            coordinates_list.append(coordinates)
            file_list.append(file[11:-4])

        # 画图
        plotAirfoilGeom(coordinates_list, file_list, save_path)
        print("二维翼型可视化图已保存在{}".format(save_path))
